package net.chathu.airgw.model.handler 
{
	import flash.events.Event;
	import flash.utils.ByteArray;
	import net.chathu.airgw.event.DataSendEvent;
	import net.chathu.airgw.model.conn.GWConnection;

	/**
	 * ...
	 * @author ze
	 */
	public class GWPacketProcessor 
	{
		
		
		private static const cmd_login:int=0x01;
		private static const cmd_startchat:int=0x02;
		private static const cmd_perfectversion:int=0x03;
		private static const cmd_sufficientversion:int=0x04;
		private static const cmd_oldversion:int=0x05;
		private static const cmd_newversion:int=0x06;
		private static const cmd_disconnectfromchat:int=0x07;
		
		
		private static const toRedir:int = 0x01;
		private static const toChatServer:int = 0x02;
		private static const toClient:int = 0x03;
		
		
		public function GWPacketProcessor() 
		{
			
		}
		
			
		public static function processGWPacket(connection:GWConnection, data:ByteArray):void
		{

			var target:int = data.readByte();
			 
			switch(target)
			{
				case toRedir:
					//trace("TO REDIRECTOR");
					processRedirectPacket(connection, data);
				break;
				
				case toChatServer:
					//trace("TO CHAT SERVER: " + data.bytesAvailable);
					var bytes:ByteArray = new ByteArray();
					data.readBytes(bytes, 0, data.bytesAvailable)
					connection.soul.chatServerConnection.sendPacket(bytes);
				break;
				
				case toClient:
					//trace("TO CLIENT");
					processClientPacket(connection, data);
				break;
				default:
					trace("UNKNOWN TARGET: " + target);
				break;
				
			}
		}
		public static function processRedirectPacket(connection:GWConnection, data:ByteArray):void
		{
			//trace("CMD DUMP:");
			//ByteArrayDump.dump(data);
			var cmd:int = data.readByte();
			
			 
			switch(cmd)
			{
				case cmd_login:
					trace("CMD LOGIN");
					var usernamelen:int = data.readByte();
					var username:String = data.readUTFBytes(usernamelen);
					connection.soul.accountData.username = username;
					connection.soul.webLogin();
					
				break;
				
				case cmd_startchat:
					trace("START CHAT");
					if (connection.connectToChat)
					{
						connection.soul.createChatConnection();
					}
					connection.dispatchEvent(new DataSendEvent("startchat"));
				break;
				
				case cmd_perfectversion:
					trace("PERFECT VERSION");
					//connection.__SEND_LOGIN_INFO();
					connection.dispatchEvent(new DataSendEvent("ready"));
				break;
				
				case cmd_sufficientversion:
					trace("cmd_sufficientversion");
					connection.dispatchEvent(new DataSendEvent("ready"));
				break;
				
				case cmd_oldversion:
					trace("cmd_oldversion");
				break;
				
				case cmd_newversion:
					trace("cmd_newversion");
				break;
				
				case cmd_disconnectfromchat:
					trace("cmd_disconnectfromchat");
				break;
				
				default:
					trace("UNKNOWN PACKET: " + cmd);
				break;
				
			}
		}
		
		public static function processClientPacket(connection:GWConnection, data:ByteArray):void
		{
			var msg:String = data.readUTFBytes(data.bytesAvailable);
			msg = msg.replace("\n", "");
			connection.dispatchEvent(new DataSendEvent("message", msg));
			trace("S>>" + msg);
			if (msg.substr(0,4)=="PING")
			{
				connection.sendClientPacket("PONG" + msg.substr(4));
			}
			
		}
		
	}

}